package com.moneyhelper.upms.vo;

import com.moneyhelper.common.util.TreeUtil;
import com.moneyhelper.common.vo.TreeNodeVO;
import com.moneyhelper.upms.entity.SysMenu;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @Author yanggld
 * @Date 2019/7/8-10:30
 * @Description
 */
@EqualsAndHashCode(callSuper=false)
@Data
public class MenuTree extends TreeNodeVO<MenuTree> {

	private String code;

	private String label;

	private Integer orderNum;

	public static List<MenuTree> buildTree(List<SysMenu> menus, String root) {
		List<MenuTree> trees = new ArrayList<>();
		MenuTree node = null;
		for (SysMenu menu : menus) {
			node = new MenuTree();
			BeanUtils.copyProperties(menu, node);
			node.setLabel(menu.getTitle());
			trees.add(node);
		}
		return TreeUtil.bulid(trees,root,  new Comparator<MenuTree>() {
			@Override
			public int compare(MenuTree o1, MenuTree o2) {
				return o1.getOrderNum() - o2.getOrderNum();
			}
		}) ;
	}
}